﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Zor_Modes;
using Zor_Tools;
using Zor_Bll;
using Newtonsoft.Json.Linq;

namespace Zor_API.Controllers
{
    public class CredentialInfoController : ApiController
    {
        /// <summary>
        /// 添加房屋证件信息
        /// </summary>
        /// <param name="ci"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage InserCredentialInfo(CredentialInfo ci)
        {
            BaseAnswer bar = new BaseAnswer();
            ci.AuditStatus = 0;
             List<CredentialInfo> credentialInfo = CredentialInfo_Bll.GetCredentialInfoList(ci);
             if (credentialInfo.Count > 0)
             {
                 bar = CredentialInfo_Bll.UpdateCredentialInfo(ci);
             }
             else 
             {
                 bar = CredentialInfo_Bll.InserCredentialInfo(ci);
             }
            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result;
        }
        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="ci"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage ApplyPushMeg(CredentialInfo ci)
        {
            ci.AuditTime = DateTime.Now;
            BaseAnswer bar = CredentialInfo_Bll.ApplyPushMeg(ci);
            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result; 
        }
        [HttpPost]
        public List<PropertyInfo> PushMegList([FromBody]PropertyInfo pi)
        {
            BaseAnswer bar = new BaseAnswer();
            List<PropertyInfo> resultlist = new List<PropertyInfo>();
            PropertyInfo resultinfo = new PropertyInfo();
            List<PropertyInfo> lstpi = PropertyInfo_Bll.GetPropertyInfoListByDtParentId(pi);
            List<CredentialInfo> CredentialInfo = CredentialInfo_Bll.GetPushMegList(pi);
            if (lstpi.Count>0)
            {
                foreach (PropertyInfo propertyInfo in lstpi)
                {
                    foreach (CredentialInfo credentialInfo in CredentialInfo)
                    {
                        if (propertyInfo.Hs_Id == credentialInfo.Property_Id) 
                        {
                            resultinfo = propertyInfo;
                            resultinfo.Property_Id = credentialInfo.Credential_Id;
                            resultinfo.Property_Card = credentialInfo.Property_Card;
                            resultinfo.Property_IDNumber = credentialInfo.Property_IDNumber;
                            resultinfo.Property_LeaseContract = credentialInfo.Property_LeaseContract;
                            resultinfo.Property_PaymentCertificate = credentialInfo.Property_PaymentCertificate;
                            resultinfo.AuditMeg = credentialInfo.AuditMeg;
                            resultinfo.AuditStatus = credentialInfo.AuditStatus;
                            resultinfo.AuditTime = credentialInfo.AuditTime;
                            resultinfo.AuditUserId = credentialInfo.AuditUserId;
                            resultinfo.AuditUserName = credentialInfo.AuditUserName;
                            resultlist.Add(resultinfo);
                        }
                    }
                    
                }
            }
            return resultlist;
        }

    }
}
